using System;
using System.Collections.Generic;
using FairyGUI;
using UIPack;
using UnityEngine;
// delegate void OnWebGLAbortedDelegate(int nativeId);


class Pop {
    private static Stack<ComConfirm> ComfirmPool = new Stack<ComConfirm>();
    public static void ShowComfirm(string text, string title, Action cbOk = null, Action cbCancel = null)
    {
        ComPopCon con;
        if (PopConArr.Count == 0)
        {
            con = ComPopCon.CreateInstance();//一个装弹出view的容器，包括一个全屏背景 和一个全屏点击区域（loader）
            con.width = GRoot.inst.width;
            con.height = GRoot.inst.height;

        }
        else
        {
            con = PopConArr.Pop();
        }

        var ld = con.Ld;
        ld.onClick.Remove(OnClickPopConBg);
        ld.visible = true;


        ComConfirm comComfirm;
        if (ComfirmPool.Count > 0)
        {
            comComfirm = ComfirmPool.Pop();
        }
        else
        {
            comComfirm = ComConfirm.CreateInstance(); //一个装弹出view的容器，包括一个全屏背景 和一个全屏点击区域（loader）
        }

        comComfirm.x = (con.width - comComfirm.width) * 0.5f;
        comComfirm.y = (con.height - comComfirm.height) * 0.5f;
        con.AddChild(comComfirm);
        ComStage.Instance.Pop.AddChild(con);

        comComfirm.Init(cbOk, cbCancel);

        comComfirm.Txt.text = text;
        comComfirm.TxtTitle.text = title;
    }

    private static Stack<ComAlert> AlertPool = new Stack<ComAlert>();
    public static void ShowAlert(string text, Action cbOk = null) {
        ComPopCon con;
        if (PopConArr.Count == 0) {
            con = ComPopCon.CreateInstance();//一个装弹出view的容器，包括一个全屏背景 和一个全屏点击区域（loader）
            con.width = GRoot.inst.width;
            con.height = GRoot.inst.height;

        }
        else {
            con = PopConArr.Pop();
        }

        var ld = con.Ld;
        ld.onClick.Remove(Pop.OnClickPopConBg);
        ld.visible = true;

        ComAlert comAlert;
        if (AlertPool.Count > 0) {
            comAlert = AlertPool.Pop();
        }
        else {
            comAlert = ComAlert.CreateInstance(); //一个装弹出view的容器，包括一个全屏背景 和一个全屏点击区域（loader）
        }

        comAlert.x = (con.width - comAlert.width) * 0.5f;
        comAlert.y = (con.height - comAlert.height) * 0.5f;
        con.AddChild(comAlert);
        ComStage.Instance.Pop.AddChild(con);

        comAlert.Init(cbOk);

        comAlert.Txt.text = text;
    }

    // private static Stack<GComponent> ComTipMsgPool = new Stack<GComponent>();
    // public static void ShowTip(string str) {
    //     GComponent tip = null;
    //     if (ComTipMsgPool.Count == 0) {
    //         tip = UIPackage.CreateObject("Common", "ComTipMsg").asCom;
    //         tip.touchable = false;
    //         tip.x = GRoot.inst.width * 0.5f;
    //         tip.y = GRoot.inst.height * 0.5f;
    //     }
    //     else {
    //         tip = ComTipMsgPool.Pop();
    //         var txt = tip.GetChild("Txt");
    //         txt.alpha = 1;
    //         txt.y = 50;
    //     }

    //     tip.GetChild("Txt").text = str;

    //     var anim = tip.GetTransition("Anim");
    //     anim.Play( ()=>{
    //         ComTipMsgPool.Push(tip);
    //         tip.parent.RemoveChild(tip);
    //     });
    //     ComStage.Instance.Kep1er.AddChild(tip);
    // }

    private static Stack<ComPopCon> PopConArr = new Stack<ComPopCon>();
    public static void PopUp(GComponent view, bool hasImgCover = true, bool hasBlockTouchCover = true, bool clickCoverClose = true) {
        ComPopCon con;
        if (PopConArr.Count == 0) {
            con = UIPackage.CreateObject("UIPack", "ComPopCon") as ComPopCon;
            con.width = GRoot.inst.width;
            con.height = GRoot.inst.height;
        }
        else {
            con = PopConArr.Pop();
        }
        var ld = con.GetChild("Ld").asLoader;
        ld.visible = hasBlockTouchCover;
        var imgCover = con.GetChild("ImgCover").asGraph;
        imgCover.visible = hasImgCover;

        if (clickCoverClose)
            ld.onClick.Set(OnClickPopConBg); //因为confirm 点这个不会关闭 confirm那段代码有可能会移除这个点击事件，所以每次都要set一个回调
        else
            ld.onClick.Remove(OnClickPopConBg);

        view.x = (con.width - view.width) * 0.5f;
        view.y = (con.height - view.height) * 0.5f;

        con.AddChild(view);

        ComStage.Instance.Pop.AddChild(con);
    }

    public static void RemovePop(GObject view) {
        if (view.parent != null) {//如果在显示列表中才删除
            var num = ComStage.Instance.Pop.numChildren;
            ComPopCon con = null;
            if (view != null) {//制定删除某一个view
                for (var i = 0; i < num; i++) {
                    var element = ComStage.Instance.Pop.GetChildAt(i).asCom;
                    if (element.GetChildAt(2) == view) {
                        con = ComStage.Instance.Pop.RemoveChildAt(i) as ComPopCon;
                        break;
                    }
                }
            }
            con.RemoveChildAt(2);//删除view view在2的位置
            Pop.PopConArr.Push(con);
        }
    }

    //删除最顶层的PopUI
    public static void RemovePopTop() {
        var num = ComStage.Instance.Pop.numChildren;
        ComPopCon con = null;
        con = ComStage.Instance.Pop.RemoveChildAt(num - 1) as ComPopCon;
        con.RemoveChildAt(2);//删除view view在2的位置
        Pop.PopConArr.Push(con);
    }

    public static void RemovePopAll() {
        while (ComStage.Instance.Pop.numChildren > 0)
            RemovePopTop();
    }

    private static void OnClickPopConBg(EventContext context) {
        Pop.RemovePopTop();


        Debug.Log("关闭");
        // var ld = context.sender as GLoader
        // ld.parent.RemoveChildAt(2)
        // ComStage.Instance.Pop.RemoveChild(ld.parent)
        // Pop.PopConArr.push(ld.parent)
    }

}